<!--  -->
<template>
  <div class="cart">
    <div class="cart-header">购物车</div>
    <div class="noData" v-if="cartLists.length==0">
      <i class="iconfont icon-gouwuche"></i>
      <p>购物车为空，请添加购物车</p>
      <mt-button type="default" @click="toHome">去逛逛</mt-button>
    </div>
    <div class="cart-list">
      <ul>
        <li v-for="(cart,index) in cartLists" :key="cart.goods_id">
          <div class="select" @click="changeSelect(cart,index)">
            <i class="iconfont icon-danxuankuang" v-if="!cart.isSelect"></i>
            <i class="iconfont icon-danxuankuangxuanzhong" v-else style="color:#f55"></i>
          </div>
          <div class="cart-img">
            <img :src="cart.goods_thumb" alt>
          </div>
          <div class="cart-content">
            <div class="cart-title">{{cart.goods_name}}</div>
            <div class="cart-wrap">
              <div class="price">{{cart.shop_price}}</div>
              <div class="num-wrap">
                <a href="javascript:;" @click="jian(index)">-</a>
                <input type="text" v-model="cart.value">
                <a href="javascript:;" @click="add(index)">+</a>
                <i class="iconfont icon-shoucang1"></i>
                <i class="iconfont icon-shanchu1" @click="dele(index)"></i>
              </div>
            </div>
          </div>
        </li>
      </ul>
    </div>
    <div class="cart-footer" v-if="cartLists.length>0">
      <div class="selectAll" @click="changeSelectAllFn">
        <i class="iconfont icon-danxuankuang" v-if="!selectAll"></i>
        <i class="iconfont icon-danxuankuangxuanzhong" v-else style="color:#f55"></i>
        全选
      </div>
      <div class="totalPrice">
        <div class="price">
          <span>合计：</span>
          <span class="price">{{totalPrice.toFixed(2)}}</span>
        </div>
        <p>不含运费，。。。。。</p>
      </div>
      <div class="buy">去结算</div>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {};
  },
  //监听属性 类似于data概念
  computed: {
    cartLists() {
      return this.$store.state.cartDatas;
    },
    selectAll() {
      return this.$store.state.selectAll;
    },
    //计算选中商品的总价
    totalPrice() {
      let price = 0;
      this.$store.state.cartDatas.map(item => {
        if (item.isSelect) {
          //商品的数量*商品的单价
          price += item.value * item.shop_price;
        }
      });
      return price;
    }
  },
  methods: {
    changeSelect(cart, index) {
      //点击商品前边的选择框--选中或者取消选中
      console.log(cart);
      cart.isSelect = !cart.isSelect;
      console.log(index);
      this.$store.commit("changeSelect", { cart: cart, index: index });
    },
    changeSelectAllFn() {
      // this.$store.commit("mutations中定义的方法");
      this.$store.commit("changeSelectAllFn");
    },
    add(index) {
      this.$store.commit("add", index);
    },
    jian(index) {
      this.$store.commit("jian", index);
    },
    dele(index) {
      this.$store.commit("dele", index);
    },
    toHome() {
      this.$router.push("/home");
    }
  }
};
</script>
<style lang="less">
.cart {
  .cart-header {
    width: 100%;
    height: 4.4rem;
    background: #f55;
    color: #fff;
    text-align: center;
    line-height: 4.4rem;
    font-size: 1.6rem;
  }
  .cart-list {
    width: 100%;
    ul {
      li {
        display: flex;
        height: 7rem;
        justify-content: space-between;
        align-items: center;
        margin: 1rem 0;
        background: #fff;
        padding: 1rem 0;
        .select {
          width: 10%;
          text-align: center;
          i {
            font-size: 2rem;
          }
        }
        .cart-img {
          margin: 0 1rem;
          img {
            width: 7rem;
            height: 7rem;
          }
        }
        .cart-content {
          width: 90%;
          width: calc(90% - 9rem);
          margin-right: 1rem;
          .cart-title {
            font-size: 1.4rem;
            height: 4rem;
            line-height: 2rem;
            overflow: hidden;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
          }
          .cart-wrap {
            display: flex;
            justify-content: space-between;
            .price {
              font-size: 1.8rem;
              color: #f55;
            }
            .num-wrap {
              input {
                width: 5rem;
                height: 3rem;
                line-height: 3rem;
                text-align: center;
              }
              a {
                display: inline-block;
                width: 3rem;
                height: 3rem;
                background: #ccc;
                text-align: center;
                line-height: 3rem;
                font-size: 1.6rem;
              }
              i {
                margin: 0 0.5rem;
              }
            }
          }
        }
      }
    }
  }
  .cart-footer {
    width: 100%;
    height: 4.4rem;
    position: fixed;
    bottom: 5rem;
    background: #fff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .selectAll {
      width: 20%;
      text-align: center;
      i {
        font-size: 2rem;
      }
    }
    .totalPrice {
      width: 40%;
      margin-left: 10%;
      .price {
        font-size: 1.6rem;
        color: #f55;
      }
    }
    .buy {
      width: 30%;
      height: 4.4rem;
      line-height: 4.4rem;
      background: #f55;
      color: #fff;
      text-align: center;
      font-size: 1.4rem;
    }
  }
  .noData {
    text-align: center;
    margin-top: 5rem;
    i {
      font-size: 20rem;
      color: #999;
    }
    p {
      width: 100%;
      font-size: 1.4rem;
      color: #666;
      line-height: 3rem;
    }
  }
}
</style>